package main

import (
	"charge_tool/netlog"
	"strings"
	"time"

	"github.com/twgh/xcgui/app"
	"github.com/twgh/xcgui/wapi/wutil"
	"github.com/twgh/xcgui/widget"
	"github.com/twgh/xcgui/window"
	"github.com/twgh/xcgui/xc"
	"github.com/twgh/xcgui/xcc"
)

// go build -ldflags="-s -w -H windowsgui"
// -H windowsgui会隐藏黑窗口，-s -w去除了符号表和调试信息，这样可以减小软件体积。
//

var (
	filename string
	filter   string
	ipEnable bool
	snEnable bool
	tmEnable bool

	fileEdit   int
	filterEdit int

	w *window.Window
	a *app.App
)

func main() {
	// 炫彩_初始化, 参数填true是启用D2D硬件加速, 效果更好. 但xp系统不支持d2d, 这时候你就得填false来关闭d2d了
	a = app.New(true)
	// 创建普通窗口, 宽300, 高200, 标题是"xcgui window", xcc.Window_Style_Default是使用默认窗口风格
	w = window.New(0, 0, 800, 200, "enng tool", 0, xcc.Window_Style_Default)

	Check1 := widget.NewButton(10, 35, 70, 30, "ip", w.Handle)
	Check2 := widget.NewButton(100, 35, 70, 30, "桩号", w.Handle)
	Check3 := widget.NewButton(190, 35, 70, 30, "时间", w.Handle)

	// 设置按钮类型
	Check1.SetTypeEx(xcc.Button_Type_Check)
	Check2.SetTypeEx(xcc.Button_Type_Check)
	Check3.SetTypeEx(xcc.Button_Type_Check)

	// 启用背景透明
	Check1.EnableBkTransparent(true)
	Check2.EnableBkTransparent(true)
	Check3.EnableBkTransparent(true)

	// 设置选中
	Check1.SetCheck(true)

	filterEdit = xc.XEdit_Create(20, 60, w.GetWidth()-40, 26, w.Handle)
	xc.XEdit_SetDefaultText(filterEdit, "请输入过滤内容以逗号分隔...")
	xc.XEle_SetBorderSize(filterEdit, 10, 0, 10, 0)

	fileEdit = xc.XEdit_Create(20, 100, w.GetWidth()-40, 26, w.Handle)
	xc.XEdit_SetDefaultText(fileEdit, "直接拖放文件到这里,并点击窗口即可")
	xc.XEle_SetBorderSize(fileEdit, 10, 0, 10, 0)
	xc.XEle_Enable(fileEdit, false)
	// xc.XBkM_SetInfo(xc.XEle_GetBkManager(hEdit), strBkm)

	okBtn := widget.NewButton(50, 140, 70, 30, "解析", w.Handle)
	okBtn.Event_BnClick(onBtnClicked)

	// 显示窗口
	w.EnableDragFiles(true)
	w.Show(true)
	xc.XWnd_EnableDragFiles(w.Handle, true)
	w.Event_DROPFILES1(onWndDropFiles)
	// 运行消息循环, 程序会被阻塞在这里不退出, 当炫彩窗口数量为0时退出
	a.Run()
	// 退出界面库释放资源
	a.Exit()
}

// int CALLBACK OnDropFiles(HDROP hDropInfo , BOOL *pbHandled)
func onWndDropFiles(HXCGUI int, hDropInfo uintptr, pbHandled *bool) int {
	// fmt.Println("***************************************拖放文件到元素***************************************")
	// 获取拖放文件到窗口时鼠标的坐标.
	// var pt xc.POINT
	// wapi.DragQueryPoint(hDropInfo, &pt)
	// fmt.Println("鼠标坐标:", pt)

	files := wutil.GetDropFiles(hDropInfo)
	if len(files) > 0 {
		filename = files[0]
		xc.XEdit_SetText(fileEdit, filename)
	}
	return 0
}

func onBtnClicked(pbHandled *bool) int {
	// a.MessageBox("提示", xc.XEdit_GetText_Temp(filterEdit), xcc.MessageBox_Flag_Ok|xcc.MessageBox_Flag_Icon_Info, w.GetHWND(), xcc.Window_Style_Modal)
	if filename == "" {
		a.MessageBox("提示", "请拖放文件到这里", xcc.MessageBox_Flag_Ok|xcc.MessageBox_Flag_Icon_Info, w.GetHWND(), xcc.Window_Style_Modal)
		return 0
	}
	idx := strings.Index(filename, ".cap")
	if idx == -1 {
		a.MessageBox("提示", "请拖放.cap文件到这里", xcc.MessageBox_Flag_Ok|xcc.MessageBox_Flag_Icon_Info, w.GetHWND(), xcc.Window_Style_Modal)
		return 0
	}

	if netlog.IsChinese(filename) {
		a.MessageBox("提示", "路径不能含中文", xcc.MessageBox_Flag_Ok|xcc.MessageBox_Flag_Icon_Info, w.GetHWND(), xcc.Window_Style_Modal)
		return 0
	}
	filter = xc.XEdit_GetText_Temp(filterEdit)
	currentTime := time.Now().Format("20060102150405")
	netlog.AnalysisNetlog(filename, currentTime+"_out.txt", filter)
	a.MessageBox("提示", "解析完成", xcc.MessageBox_Flag_Ok|xcc.MessageBox_Flag_Icon_Info, w.GetHWND(), xcc.Window_Style_Modal)
	return 0
}
